package org.example.service.impl;

import org.example.service.NestedThreadsExampleService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

@Service
public class NestedThreadsExampleServiceImpl implements NestedThreadsExampleService {

    private  List<Integer> list = new ArrayList<>();
    private  Integer zero = 1;

    public  void extracted(int finalI1, long zhuId, AtomicBoolean stopAllThreads) {
        if (!stopAllThreads.get()) {
            Long name = Thread.currentThread().getId();
            System.out.println("Inner thread " + Thread.currentThread().getName() + " is running");
            // 里面线程嵌套多线程的任务代码
            try {
                Thread.sleep(1000 + (finalI1 * 1000));
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("----------主线程id" + zhuId + "子线程id" + name+"标识:"+ finalI1);
//                                if (Math.random() < 0.1) { // 以10%的概率随机抛出异常来模拟错误情况
//                                    stopAllThreads.set(true);
//                                    System.out.println("*** An error occured in inner thread " + Thread.currentThread().getName() + "! ***");
//                                }

            if (finalI1 == 3) { // 以10%的概率随机抛出异常来模拟错误情况
                stopAllThreads.set(true);
                System.out.println("*** An error occured in inner thread " + Thread.currentThread().getName() + "! ***");
            }
            list.add(zero);
            System.out.println("---------大小" + list.size());

        }
    }
}
